package cn.csc.module.pay.mq.producer;

import cn.csc.framework.mq.core.bus.AbstractBusProducer;
import cn.csc.module.pay.message.PayOrderNotifyMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送支付订单支付成功消息
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/29 14:47
 * @modify
 */
@Slf4j
@Component
public class PayOrderNotifyProducer extends AbstractBusProducer {

    @Resource
    private StreamBridge streamBridge;


    /**
     * 发送 {@link PayOrderNotifyMessage} 消息
     *
     * @param merchantOrderId 商户订单编号
     */
    public Boolean sendPayOrderNotifyMessage(String merchantOrderId) {
        PayOrderNotifyMessage notifyMessage = new PayOrderNotifyMessage(merchantOrderId);
        Map<String, Object> headers = new HashMap<>();
        headers.put(MessageConst.PROPERTY_TAGS, "payOrderSuccessTag");
        // ORIGIN_MESSAGE_ID为数字
        headers.put(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID, merchantOrderId);
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<PayOrderNotifyMessage> message = MessageBuilder.withPayload(notifyMessage).copyHeadersIfAbsent(messageHeaders)
                .build();
        return streamBridge.send("payOrderNotify-out-0", message);
    }

}
